/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IEditorSerializer } from 'vs/workbench/common/editor';
import { Emitter } from 'vs/base/common/event';

export interface WechatUser {
	name: string;
	status: string;
	avatar: string;
}

export class WechatEditorInput extends EditorInput {
	static readonly ID = 'wechatEditorInput';

	private readonly _resource: URI;
	private _selectedUser: WechatUser | undefined;

	// 添加内容变更事件
	protected readonly _onDidChangeContents = this._register(new Emitter<void>());
	readonly onDidChangeContents = this._onDidChangeContents.event;

	constructor(selectedUser?: WechatUser) {
		super();
		console.log('[WechatEditorInput] constructor, user:', selectedUser?.name);
		// 创建一个固定的虚拟资源URI，确保总是共用同一个编辑器
		this._resource = URI.parse(`wechat://editor`);
		this._selectedUser = selectedUser;
	}

	get selectedUser(): WechatUser | undefined {
		return this._selectedUser;
	}

	setSelectedUser(user: WechatUser): void {
		console.log('[WechatEditorInput] setSelectedUser:', user.name);

		// 检查用户是否真的改变了
		const userChanged = !this._selectedUser || this._selectedUser.name !== user.name;
		this._selectedUser = user;

		// 总是触发内容变更，确保编辑器会刷新
		console.log('[WechatEditorInput] 触发UI更新');
		this._onDidChangeLabel.fire();  // 更新标题
		this._onDidChangeCapabilities.fire();
		this._onDidChangeContents.fire(); // 触发内容变更
	}

	override get typeId(): string {
		return WechatEditorInput.ID;
	}

	override get editorId(): string {
		return 'wechatEditor';
	}

	override get capabilities(): any {
		return {
			canSplitInGroup: false,
			canCloseInGroup: true
		};
	}

	override getName(): string {
		return this._selectedUser ? `${this._selectedUser.name}` : 'WeChat';
	}

	override matches(otherInput: unknown): boolean {
		// 所有WechatEditorInput实例都匹配，共用一个编辑器窗口
		if (otherInput instanceof WechatEditorInput) {
			console.log('[WechatEditorInput] matches: true (共用同一编辑器窗口)');
			return true;
		}

		return false;
	}

	override async resolve(): Promise<any> {
		return null;
	}

	override dispose(): void {
		console.log('[WechatEditorInput] dispose, user:', this._selectedUser?.name);
		super.dispose();
	}

	get resource(): URI {
		return this._resource;
	}
}

// 编辑器序列化器，用于保存和恢复编辑器状态
export class WechatEditorInputSerializer implements IEditorSerializer {
	canSerialize(editorInput: EditorInput): boolean {
		return editorInput instanceof WechatEditorInput;
	}

	serialize(editorInput: EditorInput): string | undefined {
		if (!(editorInput instanceof WechatEditorInput)) {
			return undefined;
		}
		const input = editorInput as WechatEditorInput;
		return JSON.stringify({
			typeId: WechatEditorInput.ID,
			selectedUser: input.selectedUser
		});
	}

	deserialize(instantiationService: any, serializedEditorInput: string): WechatEditorInput | undefined {
		try {
			const data = JSON.parse(serializedEditorInput);
			return new WechatEditorInput(data.selectedUser);
		} catch (error) {
			return undefined;
		}
	}
}
